package graphics.screens.playlists;

import function.search.EventListener;
import function.search.SearchKeyHandler2;
import graphics.Background;
import graphics.BootColor;
import graphics.SearchInput2;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import tool.ScreenUtils;
import boot.Boot;

public class PlaylistNameScreen extends Canvas implements CommandListener {

	private final Command okCommand = new Command("Ok", Command.OK, 1);
	private final Command cancelCommand = new Command("Cancel", Command.EXIT, 1);

	private SearchInput2 nameInput;
	private SearchKeyHandler2 keyHandler;
	
	private static final int NUM_KEY = 10;

	public PlaylistNameScreen() {
		nameInput = new SearchInput2(this, NUM_KEY, BootColor.BASIC_MENU_COLOR);
		keyHandler = new SearchKeyHandler2(NUM_KEY, nameInput, this);
		keyHandler.addListener(new InnerListener());
		
		addCommand(okCommand);
		addCommand(cancelCommand);
		setCommandListener(this);
	}

	protected void paint(Graphics g) {
		ScreenUtils.setBlackBackground(g);

		Background bg = new Background();
		bg.repaint(g);

		nameInput.repaint(g);
	}

	public void commandAction(Command command, Displayable dis) {
		if (command == okCommand) {
			String raw = keyHandler.getSearchInput();
			String title = (raw.charAt(0) + "").toUpperCase();
			title += raw.substring(1);
			Boot.savePlayslist(title);
			Boot.getInstance().readyToGo();
		} else if (command == cancelCommand) {
			Boot.getInstance().readyToGo();
		}
	}

	public void keyPressed(int keyCode) {
		keyHandler.keyPressed(keyCode);
		repaint();
	}
	
	private class InnerListener implements EventListener {

		public void processEvent(String event) {
			
		}
		
	}
}
